<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BPersonalApp
 *
 * @author jianfeng
 */
class BPersonalApp extends BApp {

    const OPERATION_CREATESpace = "createSpace";
    const OPERATION_JOINSpace = "joinSpace";
    const OPERATION_LISTMYSpace = 'mySpaces';

    /*
      public function getUIManager($style = NULL) {

      if ($this->uiManager == NULL) {
      //$uiManager = new BPersonalAppUI($this);
      $uiManager = parent::getUIManager();
      //
      //           $uiManager->addRenderView('AppTopBar', 'personalAppTopBar');
      return $uiManager;
      }
      return $this->uiManager;
      }
     * 
     */

    public function runCreateSpace($data = NULL) {
        $model = Blyn::app()->getActiveApp()->getFormModel('CreateSpaceFormModel');
        $activeSpace = Blyn::app()->getActiveSpace();
        $completeStr = Yii::t('site', 'Complete');
        $nextStr = Yii::t('site', 'Next');
        $resetStr = Yii::t('site', 'Reset');
        $cancelStr = Yii::t('site', 'Cancel');
        $spaceNameStr = Yii::t('site', 'Space Name:');
        $mySpaceIdStr = Yii::t('site', 'My Space ID:');
        $spaceCategoryStr = Yii::t('site', 'Space Category:');
        $spaceRoleStr = Yii::t('site', 'Space Role:');
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        if ($screen->isAjaxLinkClicked($completeStr) || $screen->isAjaxLinkClicked($nextStr)) {
            $summaryList = array();
            if (isset($_POST['CreateSpaceFormModel'])) {
                $model->mySpaceId = $_POST['CreateSpaceFormModel']['mySpaceId'];
                $model->spaceName = $_POST['CreateSpaceFormModel']['spaceName'];
                $model->spaceCategory = $_POST['CreateSpaceFormModel']['spaceCategory'];
                //$model->attributes = $_POST['CreateSpaceFormModel'];
            }
            if ($model->mySpaceId != NULL) {
                $mySpaceId = $model->mySpaceId;
                $summaryList[$mySpaceIdStr] = $model->mySpaceId;
            }
            if ($model->spaceName != NULL)
                $summaryList[$spaceNameStr] = $model->spaceName;
            if ($model->spaceCategory != NULL) {
                $category = new BSpaceGroup($model->spaceCategory);
                $summaryList[$spaceCategoryStr] = $category->getName();
//                    getSpaceCategoryDisplayName($category);
//                    $category->getName();
            }
            if ($model->spaceRole != NULL) {
                $role = new BRole($model->spaceRole);
                $summaryList[$spaceRoleStr] = $role->getName();
            }
            $viewData = array();
            $checkError = FALSE;
            if (isset($mySpaceId)) {
                $checkError = $this->checkMySpaceIdError($mySpaceId);
                if ($checkError) {
                    $viewData['checkError'] = $checkError;
                    $viewData['model'] = $model;
                    Blyn::app()->getActiveSpace()->addContextVariable('checkError', $checkError);
                    $updateControl->addRenderView('createSpace', 'createSpaceWithMeta', $viewData);
                }
            }

            if (!$checkError) {
                $viewData['summaryList'] = $summaryList;
                $updateControl->addRenderView('createSpace', 'createSpaceSummaryView', $viewData);
                $screen->addContextVariable('summaryList', $summaryList);
            }
            //          Blyn::app()->end();
        } else if ($screen->isAjaxLinkClicked($cancelStr)) {
            Blyn::app()->getUIManager()->gotoNode($activeSpace);
        }

        //if ($screen->isAjaxLinkClicked('Confirm')) {
        $confirmStr = Yii::t('site', 'Confirm');
        if ($sender->isAjaxLinkClicked(array('id' => 'createSpaceSummary_confirm'))) {
            $summaryList = $screen->getContextVariable('summaryList');
            if ($summaryList == NULL)
                $summaryList = array();
            $data = array();
            if (isset($summaryList[$mySpaceIdStr]))
                $data['MySpaceId'] = $summaryList[$mySpaceIdStr];
            if (isset($summaryList[$spaceNameStr]))
                $data['SpaceName'] = $summaryList[$spaceNameStr];
            if (isset($summaryList[$spaceCategoryStr]))
                $data['SpaceCategory'] = $summaryList[$spaceCategoryStr];
            if (isset($summaryList[$spaceRoleStr]))
                $data['SpaceRole'] = $summaryList[$spaceRoleStr];

            $newSpace = Blyn::app()->getCurrentUser()->createSpace($data);

            //this will calls spaces list reload for user
            //Blyn::app()->getCacheManager()->addCacheEvent(BCacheManager::Event_AddNewSpace, TRUE);

            Blyn::app()->getUIManager()->gotoNode($newSpace);
        }else if ($sender->isAjaxLinkClicked($cancelStr)) {
            $updateControl->addRenderView('createSpaceWithMeta');

            //$screenFlow->rewind();
            //$screenFlow->resetContextVariable();
            //Blyn::app()->getUIManager()->gotoScreenFlow($screenFlow);
        }
    }

    public function findSpaces($searchText = NULL) {

        $qualifiedSpaces = array();

        $spaces = BSpace::findAll();

        $userSpaces = Blyn::app()->getCurrentUser()->getUserSpaces();

        foreach ($userSpaces as $userSpace) {
            foreach ($userSpace as $id => $space) {
                unset($spaces[$id]);
            }
        }

        foreach ($spaces as $space) {

            if ($space instanceof BSpace && $space->getSpaceType()->getName() != BSpaceGroup::Group_SpaceType_PersonalSpace) {
                $allowJoinRoles = $space->getSpaceEngine()->getAllowJoinSpaceRoles();
                if ($allowJoinRoles != NULL)
                    $qualifiedSpaces[$space->getId()] = $space;
                /*
                  $user = Blyn::app()->getCurrentUser();
                  if ($user->isMemberOfRoles($allowJoinRoles, TRUE))
                  $qualifiedSpaces[$space->getId()] = $space;
                 * 
                 */
            }
        }

//        $userSpaces = Blyn::app()->getCurrentUser()->getUserSpaces();
//
//        foreach ($userSpaces as $spaces) {
//            foreach ($spaces as $id => $space) {                
//                unset($qualifiedSpaces[$id]);
//            }
//        }

        return $qualifiedSpaces;
    }

    public function runMySpaces($data = NULL) {
        
    }

    /*
      public function run($data = NULL) {
      parent::run($data);

      $mapTable = new BSheet('testTable',  Blyn::app());
      $list = $mapTable->getList(array('itemData' => array('col1' => 'value1')));
      $item = $list->getItem('col1');
      $value = $item->getValue();
      }
     * 
     */

    public function runJoinSpace($data = NULL) {
        if (yii::app()->request->isAjaxRequest) {
            $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
            $senderControl = Blyn::app()->getUIManager()->getSenderControl();
//            $spaceId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('spaceId');
            $spaceId = Blyn::app()->getUIManager()->getCurrentScreen()->getContextVariable('spaceId');
            if ($senderControl->isAjaxLinkClicked('linkJoin')) {
                $spaceId = Blyn::app()->getUIManager()->getActiveLink()->getLinkVariable('spaceId');
                $model = Blyn::app()->getActiveApp()->getFormModel('JoinSpaceFormModel');
                if (isset($_POST['optionsRadios'])) {
                    $checkedRoleId = $_POST['optionsRadios'];
                }
                if ($spaceId)
                    $updateControl->addRenderView('modalPage', 'applyJoinSpace_modal_content', array('spaceId' => $spaceId));
            }
        }
    }

    /*
      public function runJoinSpace($data=NULL){
      $spaceCooker = new BSpaceCooker();
      $spaceCooker->onShowSpaceListForJoin();
      }
     * 
     */

    protected function checkMySpaceIdError($mySpaceId) {
        $space = BSpace::find(array('myId' => $mySpaceId));
        if ($space instanceof BSpace && $space->getId() != NULL) {
            return yii::t('site', 'my space ID exist already!');
        }
        return FALSE;
    }

}

?>
